var $;
var table
var layer;
layui.use(['table', 'form', 'layer', 'jquery'], function () {
    $ = layui.jquery;
    table = layui.table;
    layer = parent.layui.layer;


    var index = layer.load(1, {
        shade: [0.3, '#000']
    });

    top.interfaceTable = table.render({
        elem: '#interface_table'
        , limit: 5
        , limits: [5]
        , page: true
        , where: {/*查询条件,暂不查询*/}
        , url: '/admin/interface/list' //数据接口
        , page: true //开启分页
        , cols: [[ //表头
            {field: 'code', title: '权限值', align: 'center', sort: true, width: 80}
            , {field: 'serviceName', title: '所属服务', align: 'center'}
            , {field: 'uri', title: '接口', align: 'center', width: 240}
            , {field: 'memo', title: '说明', align: 'center', sort: true, minWidth: 200}
            , {field: 'createTime', title: '创建时间', align: 'center', sort: true}
            , {
                field: 'createDateStr', title: '操作', align: 'center', width: 160, templet: function (el) {
                    return '<a class="layui-btn layui-btn-sm layui-btn-radius" ' +
                        "onclick=info(" + el.code + ",\"" + el.serviceName + "\",\"" + el.uri + "\",\"" + el.memo + "\",\"" + el.createTime + "\",\"" + el.modifyTime + "\") title='查看详情'>详&nbsp;情</a>"
                        + '<a class="layui-btn layui-btn-sm layui-btn-radius layui-btn-danger" ' +
                        'onclick=remove("' + el.code + '") title="删除">删&nbsp;除</a>';
                }
            }
        ]]
    });

    //填充select数据
    $.ajax({
        url: '/admin/interface/allService',
        datatype: 'json',
        success: function (res) {
            if (res.code == 0) {
                var dat = res.data;
                var options = '<option value="">全部</option>';
                for (var i = 0; i < dat.length; i++) {
                    options += '<option value="' + dat[i].id + '">' + dat[i].serviceName + '</option>';
                }
                $('#select_service').html(options)
                layui.form.render('select');
            }
        }, complete: function (res) {
            parent.layer.close(index);
        }
    });

    //监听数据表格中的应用名下拉选
    layui.form.on('select(service)', function (data) {
        var index = parent.layer.load(1, {
            shade: [0.3, '#000']
        });
        var serviceId = $('#select_service').val();
        if (serviceId == '') {
            serviceId = null;
        }
        table.reload('interface_table', {
            url: '/admin/interface/list',
            where: {
                "serviceId": serviceId
            }
        });
        parent.layer.close(index);

    });
});

function toAddInterface() {
    layui.use(['layer', 'table'], function () {
        var layer = parent.layui.layer;
        top.interfaceId = layer.open({
            title: '新增接口',
            type: 2,
            area: ['720px', '480px'],
            content: '/admin/interfaceAdd.html'
        });
    });
}

function condition(){
    var code = $('#code').val();
    if(code.trim().length==0){
        code = null;
    }
    return {
        "id":code
        ,"serviceId":$('#select_service').val()
    }
}

function refreshTable() {
    layui.use(['layer', 'table'], function () {
        table.reload('interface_table', {
            url: '/admin/interface/list',
            where:condition()
        }, true);
    });
}

function info(code, serviceName, uri, memo, createTime, modifyTime) {
    top.interfaceId = layer.open({
        title: '用户详情',
        type: 2,
        area: ['540px', '480px'],
        content: '/admin/interfaceInfo.html',
        success: function (layero, index) {
            var body = layer.getChildFrame('body', index);
            var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
            body.find('#code').val(code);
            body.find('#serviceName').val(serviceName);
            body.find('#uri').val(uri);
            body.find('#memo').val(memo);
            body.find('#createTime').val(createTime);
        },
        cancel: function () {
            return;
        }
        , end: function () {
            //   refreshTable();
        }
    });
}

function remove(id) {
    var index = layer.confirm('真的要<span style="color: red">删除权限值为'+id+'</span>的接口吗？', {
        btn: ['确定', '取消'] //按钮
    }, function () {
        $.ajax({
            url: '/admin/interface/delete',
            datatype: 'json',
            data:{
                "id":id
            },
            success: function (res) {
                if (res.code == 0) {
                    parent.layer.msg(res.msg, {icon: 1});
                    top.interfaceTable.reload();
                }else{
                    parent.layer.msg(res.msg, {icon: 2});
                }
            }, complete: function (res) {
                parent.layer.close(index);
            }
        });
    }, function () {});
}


function query(){
    refreshTable();
}


